package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.MobileStaffPermission;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 员工权限 Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2024-06-26
 */
public interface MobileStaffPermissionMapper extends BaseMapper<MobileStaffPermission> {

    @Select({"<script>",
            "select `code` from mobile_permission where code &lt;&gt; '' and code  is not null",
            "<when test='type!=null and type!=&apos;&apos;'>",
            "and type = ${type}",
            "</when>",
            "</script>"})
    public List<String> getPermissionCodeAll(@Param("type") Integer type);

    /**
     * 查询个人权限
     *
     * @param staffId
     * @return
     */
    @Select({"<script>",
            "SELECT ",
            "`code` ",
            "FROM ",
            "mobile_permission ",
            "WHERE ",
            "id IN ( ",
            "SELECT DISTINCT permission_id ",
            "FROM ",
            "mobile_role_permission ",
            "WHERE ",
            "role_id IN ( ",
            "SELECT ",
            "role_id ",
            "FROM ",
            "role_admin ",
            "WHERE ",
            "admin_id = ( SELECT admin_id FROM staff WHERE id = ${staffId} )) ",
            "AND permission_id NOT IN ( SELECT permission_id FROM mobile_staff_permission WHERE staff_id = ${staffId} AND type = 2 ) ",
            "UNION ",
            "SELECT permission_id ",
            "FROM ",
            "mobile_staff_permission",
            "WHERE ",
            "staff_id = ${staffId} ",
            "AND type = 1 ",
            ") ",
            "AND type = ${type} ",
            "</script>"})
    public List<String> getStaffPermissionAllByStaffId(@Param("staffId") Long staffId, @Param("type") Integer type);


    @Select({"<script>",
            "SELECT ",
            "`id` ",
            "FROM ",
            "mobile_permission ",
            "WHERE ",
            "id IN ( ",
            "SELECT DISTINCT permission_id ",
            "FROM ",
            "mobile_role_permission ",
            "WHERE ",
            "role_id IN ( ",
            "SELECT ",
            "role_id ",
            "FROM ",
            "role_admin ",
            "WHERE ",
            "admin_id = ( SELECT admin_id FROM staff WHERE id = ${staffId} )) ",
            "AND permission_id NOT IN ( SELECT permission_id FROM mobile_staff_permission WHERE staff_id = ${staffId} AND type = 2 ) ",
            "UNION ",
            "SELECT permission_id ",
            "FROM ",
            "mobile_staff_permission",
            "WHERE ",
            "staff_id = ${staffId} ",
            "AND type = 1 ",
            ") ",
            "AND type = ${type} ",
            "</script>"})
    public List<Long> getStaffPermissionAllIdByStaffId(@Param("staffId") Long staffId, @Param("type") Integer type);

}
